package com.hjt.frame;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.hjt.adb.utils.DeviceUtil;
import com.hjt.dto.MemberInfo;
import com.hjt.frame.panels.OverwriteFrame;
import com.hjt.frame.panels.RoleLabel;
import com.hjt.frame.panels.RolePanel;

import cn.hutool.core.collection.ListUtil;

public class MainFrame extends JFrame {
	
	public static Toolkit toolkit;
	private final Integer w = 500;
	private final Integer h = 600;
	private Double textAreaNum = 0d;
	public static JRadioButton scriptMode;
	public static List<MemberInfo> members = new ArrayList<>();

	public MainFrame() {
		loadData();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(null);
		this.setUndecorated(true);
		toolkit = Toolkit.getDefaultToolkit();
		Dimension d = toolkit.getScreenSize();
		this.setBounds(Double.valueOf(d.getWidth()).intValue() - w - 50, Double.valueOf(d.getHeight()).intValue() - h - 150, w, h);
		MouseDragListener listener = new MouseDragListener(this);
		this.addMouseMotionListener(listener);
		this.addMouseListener(listener);
		List<JTextArea> AllAreas = addText(ListUtil.of("998", "574"), "模拟器宽度", "模拟器高度");
		List<JTextArea> gameAreas = addText(ListUtil.of("960", "540"), "模拟器可见区宽度", "模拟器可见区高度");
		modeSwitch();
		JTextArea nameArea = addText(null, "获取游戏名称位置").get(0);
		nameArea.setText("x,y,w,h");
		JButton b = addButton("开启");
		RolePanel panel = new RolePanel();
		new RoleLabel(this, panel, textAreaNum);
		this.add(panel);
		b.addMouseListener(new ButtonListener(b, AllAreas, gameAreas));
		this.setVisible(true);
	}
	
	private void loadData() {
		List<String> devices = DeviceUtil.devices();
		for(String dev: devices) {
			MemberInfo memberInfo = new MemberInfo();
			memberInfo.setLabel(dev);
			memberInfo.setDevice(dev);
			BufferedImage buff = DeviceUtil.screenCap(dev);
			memberInfo.setWidth(buff.getWidth());
			memberInfo.setHeight(buff.getHeight());
			members.add(memberInfo);
		}
	}
	
	private void modeSwitch() {
		scriptMode = new JRadioButton();
		JLabel label = new JLabel();
		label.setText("录屏/同步模式：");
		label.setBounds(10 + 220 * 1, 60 + Double.valueOf(textAreaNum * 30).intValue(), 120, 20);
		scriptMode.setBounds(130 + 220 * 1, 60 + Double.valueOf(textAreaNum * 30).intValue(), 80, 20);
		this.add(label);
		this.add(scriptMode);
	}
	
	private JButton addButton(String text) {
		JButton area = new JButton();
		area.setBounds(350, 60 + Double.valueOf(textAreaNum * 30).intValue(), 80, 30);
		area.setText(text);
		this.add(area);
		textAreaNum = textAreaNum + 1.5;
		return area;
	}
	
	private List<JTextArea> addText(List<String> values, String...labelText) {
		int num = 0;
		List<JTextArea> rs = new ArrayList<>();
		for(int i = 0; i < labelText.length; i++) {
			String text = labelText[0];
			JTextArea area = new JTextArea();
			if(values != null && values.size() > i) {
				area.setText(values.get(i));
			}
			JLabel label = new JLabel();
			label.setText(text + "：");
			label.setBounds(10 + 220 * num, 60 + Double.valueOf(textAreaNum * 30).intValue(), 120, 20);
			area.setBounds(130 + 220 * num, 60 + Double.valueOf(textAreaNum * 30).intValue(), 80, 20);
			this.add(label);
			this.add(area);
			rs.add(area);
			num++;
		}
		textAreaNum ++;
		return rs;
	}
	
	@Override
	public void paint(Graphics g) {
		g.setColor(new Color(245, 245, 245));
		g.fillRect(0, 0, w, h);
		super.paint(g);
		g.setColor(new Color(46, 46, 46));
		g.fillRect(0, 0, w, 30);
		g.setColor(new Color(151, 151, 151));
		g.drawString("辅助脚本", 10, 20);

		g.drawString(String.format("当前屏幕宽度：%s px", toolkit.getScreenSize().width), 10, 50);
		g.drawString(String.format("当前屏幕高度：%s px", toolkit.getScreenSize().height), 170, 50);
		
		g.setColor(new Color(197, 206, 216));
		g.drawLine(0, 0, 0, h);
		g.drawLine(w-1, 0, w-1, h);
		g.drawLine(0, h-1, w, h-1);
		g.setColor(new Color(151, 151, 151));
		g.drawString("close", w - 40, 20);
	}
	
	public static void main(String[] args) {
		String lookAndFeel ="com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; 
		try {
			UIManager.setLookAndFeel(lookAndFeel);
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
				| UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		new MainFrame();
	}
	
	class ButtonListener extends MouseAdapter {
		private JButton b;
		private List<JTextArea> AllAreas;
		private List<JTextArea> gameAreas;
		private OverwriteFrame overwriteFrame;
		
		public ButtonListener(JButton b, List<JTextArea> AllAreas, List<JTextArea> gameAreas) {
			this.b = b;
			this.AllAreas = AllAreas;
			this.gameAreas = gameAreas;
		}
		
		@Override
		public void mouseClicked(MouseEvent e) {
			if(overwriteFrame == null) {
				int w = Integer.parseInt(AllAreas.get(0).getText());
				int h = Integer.parseInt(AllAreas.get(1).getText());
				int gameW = Integer.parseInt(gameAreas.get(0).getText());
				int gameH = Integer.parseInt(gameAreas.get(1).getText());
				overwriteFrame = new OverwriteFrame(0, h - gameH, gameW, gameH);
			}
			if(b.getText().equals("开启")) {
				b.setText("关闭");
				int w = Integer.parseInt(AllAreas.get(0).getText());
				int h = Integer.parseInt(AllAreas.get(1).getText());
				int gameW = Integer.parseInt(gameAreas.get(0).getText());
				int gameH = Integer.parseInt(gameAreas.get(1).getText());
				overwriteFrame.setBounds(0, h - gameH, gameW, gameH);
				overwriteFrame.setVisible(true);
			}else {
				b.setText("开启");
				overwriteFrame.setVisible(false);
			}
		}
	}

	class MouseDragListener extends MouseAdapter{
		private JFrame frame;
		private Boolean isDrag = false;
		private int x,y;
		
		public MouseDragListener(JFrame frame) {
			this.frame = frame;
		}
		@Override
		public void mouseDragged(MouseEvent e) {
			if(!isDrag) {
				isDrag = true;
				x = e.getXOnScreen();
				y = e.getYOnScreen();
				return;
			}
			int nX = e.getXOnScreen();
			int nY = e.getYOnScreen();
			Point old = frame.getLocation();
			Double ox = old.getX();
			Double oy = old.getY();
			frame.setLocation(ox.intValue() + (nX - x), oy.intValue() + (nY -y));
			x = nX;
			y = nY;
			
		}
		@Override
		public void mouseClicked(MouseEvent e) {
			int eX = e.getX();
			int eY = e.getY();
			if(eX > (w - 50) && eY <= 30) {
				System.exit(0);
			}
		}
		
		@Override
		public void mouseExited(MouseEvent e) {
			isDrag = false;
		}
		
		@Override
		public void mouseReleased(MouseEvent e) {
			isDrag = false;
		}
	}
}

